library(readr)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5 ✓ dplyr 1.0.7
## ✓ tibble 3.1.4 ✓ stringr 1.4.0
## ✓ tidyr 1.1.3 ✓ forcats 0.5.1
## ✓ purrr 0.3.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(mapview)
## Warning: multiple methods tables found for 'crop'
## Warning: multiple methods tables found for 'extend'
library(sf)
## Linking to GEOS 3.8.1, GDAL 3.2.1, PROJ 7.2.1
library(tigris)
## To enable
## caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
library(leaflet)
library(censusapi)
##
## Attaching package: 'censusapi'
## The following object is masked from 'package:methods':
##
## getFunction
library(devtools)
## Loading required package: usethis
#claims_public <- read_csv(file="/Users/pacopoler/Desktop/s22-218z-fema/FimaNfipClaims.csv")
#claims_public <- read_csv("/Volumes/GoogleDrive/Shared drives/S22 218Z/FEMA/Data/Fema) Downloads/FimaNfipClaims.csv")
claims_public <- readRDS("claims.rds")
bay_county_names <-
c(
"Alameda",
"Contra Costa",
"Marin",
"Napa",
"San Francisco",
"San Mateo",
"Santa Clara",
"Solano",
"Sonoma"
)
CA_counties <- counties("CA")
##
|
| | 0%
|
| | 1%
|
|= | 1%
|
|= | 2%
|
|== | 2%
|
|== | 3%
|
|== | 4%
|
|=== | 4%
|
|=== | 5%
|
|==== | 5%
|
|==== | 6%
|
|===== | 7%
|
|===== | 8%
|
|====== | 8%
|
|====== | 9%
|
|======= | 9%
|
|======= | 10%
|
|======= | 11%
|
|======== | 11%
|
|======== | 12%
|
|========= | 12%
|
|========= | 13%
|
|========= | 14%
|
|========== | 14%
|
|========== | 15%
|
|=========== | 15%
|
|=========== | 16%
|
|============ | 16%
|
|============ | 17%
|
|============ | 18%
|
|============= | 18%
|
|============= | 19%
|
|============== | 19%
|
|============== | 20%
|
|============== | 21%
|
|=============== | 21%
|
|=============== | 22%
|
|================ | 22%
|
|================ | 23%
|
|================= | 24%
|
|================= | 25%
|
|================== | 25%
|
|================== | 26%
|
|=================== | 26%
|
|=================== | 27%
|
|=================== | 28%
|
|==================== | 28%
|
|==================== | 29%
|
|===================== | 29%
|
|===================== | 30%
|
|===================== | 31%
|
|====================== | 31%
|
|====================== | 32%
|
|======================= | 32%
|
|======================= | 33%
|
|======================== | 34%
|
|======================== | 35%
|
|========================= | 35%
|
|========================= | 36%
|
|========================== | 36%
|
|========================== | 37%
|
|========================== | 38%
|
|=========================== | 38%
|
|=========================== | 39%
|
|============================ | 39%
|
|============================ | 40%
|
|============================= | 41%
|
|============================= | 42%
|
|============================== | 42%
|
|============================== | 43%
|
|============================== | 44%
|
|=============================== | 44%
|
|=============================== | 45%
|
|================================ | 45%
|
|================================ | 46%
|
|================================= | 46%
|
|================================= | 47%
|
|================================= | 48%
|
|================================== | 48%
|
|================================== | 49%
|
|=================================== | 49%
|
|=================================== | 50%
|
|=================================== | 51%
|
|==================================== | 51%
|
|==================================== | 52%
|
|===================================== | 52%
|
|===================================== | 53%
|
|====================================== | 54%
|
|====================================== | 55%
|
|======================================= | 55%
|
|======================================= | 56%
|
|======================================== | 56%
|
|======================================== | 57%
|
|======================================== | 58%
|
|========================================= | 58%
|
|========================================= | 59%
|
|========================================== | 59%
|
|========================================== | 60%
|
|========================================== | 61%
|
|=========================================== | 61%
|
|=========================================== | 62%
|
|============================================ | 62%
|
|============================================ | 63%
|
|============================================= | 64%
|
|============================================= | 65%
|
|============================================== | 65%
|
|============================================== | 66%
|
|=============================================== | 66%
|
|=============================================== | 67%
|
|=============================================== | 68%
|
|================================================ | 68%
|
|================================================ | 69%
|
|================================================= | 69%
|
|================================================= | 70%
|
|================================================= | 71%
|
|================================================== | 71%
|
|================================================== | 72%
|
|=================================================== | 72%
|
|=================================================== | 73%
|
|=================================================== | 74%
|
|==================================================== | 74%
|
|==================================================== | 75%
|
|===================================================== | 75%
|
|===================================================== | 76%
|
|====================================================== | 76%
|
|====================================================== | 77%
|
|====================================================== | 78%
|
|======================================================= | 78%
|
|======================================================= | 79%
|
|======================================================== | 79%
|
|======================================================== | 80%
|
|======================================================== | 81%
|
|========================================================= | 81%
|
|========================================================= | 82%
|
|========================================================== | 82%
|
|========================================================== | 83%
|
|========================================================== | 84%
|
|=========================================================== | 84%
|
|=========================================================== | 85%
|
|============================================================ | 85%
|
|============================================================ | 86%
|
|============================================================= | 86%
|
|============================================================= | 87%
|
|============================================================= | 88%
|
|============================================================== | 88%
|
|============================================================== | 89%
|
|=============================================================== | 89%
|
|=============================================================== | 90%
|
|=============================================================== | 91%
|
|================================================================ | 91%
|
|================================================================ | 92%
|
|================================================================= | 92%
|
|================================================================= | 93%
|
|================================================================== | 94%
|
|================================================================== | 95%
|
|=================================================================== | 95%
|
|=================================================================== | 96%
|
|==================================================================== | 97%
|
|===================================================================== | 98%
|
|===================================================================== | 99%
|
|======================================================================| 99%
|
|======================================================================| 100%
bay_counties <- filter(CA_counties, NAME %in% bay_county_names) %>%
rename('countyCode' = 'GEOID') %>%
select(countyCode, NAME) %>%
mutate(NAME = toupper(NAME)) %>%
st_drop_geometry()
bay_public <- filter(claims_public, countyCode %in% bay_counties$countyCode) %>%
select(censusTract, countyCode, yearOfLoss, amountPaidOnBuildingClaim) %>%
rename('year' = 'yearOfLoss') %>%
group_by(censusTract, year) %>%
summarize(
total_claims = n(),
total_claim_value = sum(amountPaidOnBuildingClaim, na.rm = T),
capped_claims = min(n(), 200),
countyCode = countyCode[which.max(countyCode)]
) %>%
left_join(bay_counties) %>%
mutate(
# NAME = (toupper(NAME) %>% paste('COUNTY', sep=' ')),
average_claim_value = total_claim_value / total_claims
)
## `summarise()` has grouped output by 'censusTract'. You can override using the `.groups` argument.
## Joining, by = "countyCode"
# NOW IMPORT CENSUS DATA
Sys.setenv(CENSUS_KEY="c8aa67e4086b4b5ce3a8717f59faa9a28f611dab")
pop_2020 <- tibble()
for (county in c(
"state:06+county:097",
"state:06+county:075",
"state:06+county:041",
"state:06+county:055",
"state:06+county:095",
"state:06+county:013",
"state:06+county:085",
"state:06+county:001",
"state:06+county:081"
)) {
pop_2020 <- rbind(pop_2020, (getCensus(
name = "dec/pl",
vintage = 2010,
region = "tract:*",
regionin = county,
# vars = "P1_001N"
vars = "P001001"
) %>%
transmute(
GEOID =
paste0(state,county,tract),
pop = P001001
)
)
)
}
# bay area census visualization
tracts_2020 <- tibble()
for (county in bay_counties$NAME){
tracts_2020 <- rbind(
tracts_2020,
tracts("CA", county, year = 2010, progress_bar = F)
)
}
pop_2020 <- pop_2020 %>%
left_join(
tracts_2020 %>%
select(GEOID10, geometry) %>%
rename('GEOID' = 'GEOID10')
) %>%
st_as_sf()
## Joining, by = "GEOID"
ggplot(pop_2020) +
geom_sf(aes(geometry=geometry, fill = pop))
pop_pal <- colorNumeric(
palette = "Blues",
domain = pop_2020$pop
)
leaflet() %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addPolygons(
data = pop_2020,
fillColor = ~pop_pal(pop),
color = "white",
weight = 0.5,
fillOpacity = 0.5,
label = ~pop
) %>%
addLegend(
position = "bottomright",
pal = pop_pal,
values = pop_2020$pop,
title = "Bay Area Population - 2020"
)
## Warning: sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
## Need '+proj=longlat +datum=WGS84'